package com.shycloud.mido.music.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.common.security.util.SecurityUtils;
import com.shycloud.mido.music.domain.MisMyCollection;
import com.shycloud.mido.music.domain.MisMyCollectionMusic;
import com.shycloud.mido.music.service.MisMyCollectionMusicService;
import com.shycloud.mido.music.service.MisMyCollectionService;
import com.shycloud.mido.music.util.CommonString;
import com.shycloud.mido.music.vo.MyCollectionVO;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 自制曲集
 *
 * @author nianhua.jiang
 * @description
 * @date 2020/5/8
 */
@RestController
@RequestMapping("myCollection")
@AllArgsConstructor
@ApiIgnore
public class MyCollectionController {

	/** 自定义曲集 service */
	private MisMyCollectionService misMyCollectionService;
	/** 自定义曲集与曲目关系 service */
	private MisMyCollectionMusicService misMyCollectionMusicService;

	/**
	 * 新建/修改自制曲集
	 *
	 * @param misMyCollection
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/5/8 17:03
	 */
	@PostMapping("saveCollection")
	@ApiOperation("新建/修改自制曲集")
	public R saveCollection(@RequestBody MisMyCollection misMyCollection) {

		//member_id
		misMyCollection.setMemberId(SecurityUtils.getMember().getId());
		//逻辑删除
		misMyCollection.setDelFlag(CommonString.DELETE_FLG);
		//更新日期
		misMyCollection.setUpdateTime(new Date());
		//创建时间
		misMyCollection.setCreateTime(new Date());

		//更新
		if (misMyCollection.getId() != null) {

			//判断自建曲集名称是否重复
			List<MisMyCollection> misMyCollectionList = misMyCollectionService.list(new QueryWrapper<MisMyCollection>()
					.eq(MisMyCollection.COL_MEMBER_ID, misMyCollection.getMemberId())
					.and(y -> y.ne(MisMyCollection.COL_ID, misMyCollection.getId()))
					.and(x -> x.eq(MisMyCollection.COL_DEL_FLAG, CommonString.DELETE_FLG)));

			for (MisMyCollection myCollection : misMyCollectionList) {
				if (misMyCollection.getName().equals(myCollection.getName())) {
					return new R(CommonConstants.FAIL, "曲集名称已存在", false);
				}
			}

			return new R(CommonConstants.SUCCESS, "更新成功", misMyCollectionService.updateById(misMyCollection));
		}

		//新增
		else {

			//判断自建曲集名称是否重复
			List<MisMyCollection> misMyCollectionList = misMyCollectionService.list(new QueryWrapper<MisMyCollection>()
					.eq(MisMyCollection.COL_MEMBER_ID, misMyCollection.getMemberId())
					.and(x -> x.eq(MisMyCollection.COL_DEL_FLAG, CommonString.DELETE_FLG)));

			for (MisMyCollection myCollection : misMyCollectionList) {
				if (misMyCollection.getName().equals(myCollection.getName())) {
					return new R(CommonConstants.FAIL, "曲集名称已存在", false);
				}
			}

			misMyCollectionService.save(misMyCollection);
			//将单条记录查询返回给前端
			MyCollectionVO myCollectionVO = misMyCollectionService.getMyCollectionSaveAfter(misMyCollection.getId());
			return new R(CommonConstants.SUCCESS, "新增成功", myCollectionVO);
		}
	}


	/**
	 * 删除曲集
	 *
	 * @param idArr
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/5/9 9:37
	 */
	@PostMapping("delCollection")
	@ApiOperation("删除曲集")
	public R delCollection(@RequestBody Map<String, List<Integer>> idArr) {

		for (Integer id : idArr.get("idArr")) {
			misMyCollectionService.removeById(id);
		}

		return new R(true);
	}

	/**
	 * 获取自定义曲集
	 *
	 * @param current
	 * @param size
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/5/9 11:06
	 */
	@GetMapping("getCollection")
	@ApiOperation("获取自定义曲集")
	public R getCollection(Integer current, Integer size) {

		//根据请求的access_token获取用户ID
		String userId = SecurityUtils.getMember().getId();

		Page page = new Page(current, size);
		IPage<MyCollectionVO> myCollection = misMyCollectionService.getMyCollection(page, userId);
		return new R(myCollection);
	}

	/**
	 * 添加曲目到我的曲集
	 *
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/5/9 13:11
	 */
	@PostMapping("addMusic")
	@ApiOperation("添加曲目到我的曲集")
	public R addMusic(@RequestBody Map<String, Object> map) {

		//自制曲集ID
		Integer myCollectionId = (Integer) map.get("myCollectionId");
		//批量添加的曲目ID集合
		List<Integer> musicIdList = (List<Integer>) map.get("musicId");

		for (Integer musicId : musicIdList) {

			//判断此曲目是否已经添加进自建曲集
			List<MisMyCollectionMusic> colMusList = misMyCollectionMusicService.list(new QueryWrapper<MisMyCollectionMusic>()
					.eq(MisMyCollectionMusic.COL_MUSIC_ID, musicId)
					.and(x -> x.eq(MisMyCollectionMusic.COL_MY_COLLECTION_ID, myCollectionId))
					.and(y -> y.eq(MisMyCollectionMusic.COL_DEL_FLAG, CommonString.DELETE_FLG)));

			//如果时单曲添加，给出提示信息
			if (musicIdList.size() == 1 && colMusList.size() != 0) {
				return new R(CommonConstants.FAIL, "该曲目已存在", false);
			}

			//如果不存在，则添加
			if (colMusList.size() == 0) {
				//创建自定义曲集与曲目关系对象
				MisMyCollectionMusic misMyCollectionMusic = new MisMyCollectionMusic();
				//创建人
				misMyCollectionMusic.setCreateUser(SecurityUtils.getMember().getId());
				//创建时间
				misMyCollectionMusic.setCreateTime(new Date());
				//更新人
				misMyCollectionMusic.setUpdateUser(SecurityUtils.getMember().getId());
				//更新时间
				misMyCollectionMusic.setUpdateTime(new Date());
				//自定义曲集ID
				misMyCollectionMusic.setMyCollectionId(myCollectionId);
				//添加的曲目ID
				misMyCollectionMusic.setMusicId(musicId);
				//逻辑删除
				misMyCollectionMusic.setDelFlag(CommonString.DELETE_FLG);
				//插入数据
				misMyCollectionMusicService.save(misMyCollectionMusic);
			}
		}

		//执行新增操作并返回结果
		return new R(true, "操作成功");

	}

	/**
	 * 从我的曲集中移除曲目
	 *
	 * @param map
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/5/9 13:26
	 */
	@PostMapping("delMusic")
	@ApiOperation("从我的曲集中移除曲目")
	public R delMusic(@RequestBody Map<String, Object> map) {

		Integer myCollectionId = (Integer) map.get("myCollectionId");
		List<Integer> musicIdArr = (List<Integer>) map.get("musicIdArr");

		for (Integer musicId : musicIdArr) {
			misMyCollectionMusicService.remove(new QueryWrapper<MisMyCollectionMusic>()
					.eq(MisMyCollectionMusic.COL_MY_COLLECTION_ID, myCollectionId)
					.and(x -> x.eq(MisMyCollectionMusic.COL_MUSIC_ID, musicId)));
		}
		return new R();
	}

	/**
	 * 自建曲集添加曲目列表
	 *
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/9/1 13:17
	 **/
	@GetMapping("getAddMusicList")
	public R getAddMusicList() {
		return new R(misMyCollectionMusicService.getAddMusicList());
	}

}
